package com.liruan.rest.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringHelper
{
	public static String underscoreToHumpStyle(String src, boolean includeFirst)
	{
		if (src == null)
		{
			return src;
		}

		String[] tmp = src.split("_", -1);
		String res = tmp[0];
		if (includeFirst)
		{
			char first = res.charAt(0);
			res = Character.toUpperCase(first) + res.substring(1, res.length());
		}
		
		for (int i = 1; i < tmp.length; i++)
		{
			String word = tmp[i];
			char first = word.charAt(0);
			res += Character.toUpperCase(first) + word.substring(1, word.length());
		}
		return res;
	}

	public static String humpToUnderscoreStyle(String src)
	{
		Pattern pattern = Pattern.compile("([A-Z])");
		String res = new String(src);
		Matcher matcher = pattern.matcher(res);
		while (matcher.find())
		{

			char first = matcher.group(1).charAt(0);
			res = matcher.replaceFirst("_" + Character.toLowerCase(first));
			matcher = pattern.matcher(res);
		}

		return res;
	}
}
